//
//  FGVideoMessageCellData.m
//  FGChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "FGVideoMessageCellData.h"
#import "FGPathTool.h"
#import "FGChatHelper.h"

@implementation FGVideoItem

- (NSString *)getName {
    NSString *name = [NSString stringWithFormat:@"%@.%@", self.uuid, self.type];
    return name;
}

- (NSString *)getPath:(NSString *)convId {
    return [FGChatHelper pathConvVideo:convId name:[self getName]];
}

- (void)downloadPath:(NSString *)path progress:(FGDownloadProgress)progress succ:(FGSucc)succ fail:(FGFail)fail {
    // TODO
}

@end

@implementation FGSnapshotItem

- (NSString *)getName {
    return [self.uuid stringByAppendingString:@"_snapshot"];
}

- (NSString *)getPath:(NSString *)convId {
    return [FGChatHelper pathConvVideo:convId name:[self getName]];
}

-(void)downloadPath:(NSString *)path progress:(FGDownloadProgress)progress succ:(FGSucc)succ fail:(FGFail)fail {
    // TODO
}


@end

@interface FGVideoMessageCellData ()

@property (nonatomic, assign) BOOL isDownloadingSnapshot;
@property (nonatomic, assign) BOOL isDownloadingVideo;

@end

@implementation FGVideoMessageCellData

+ (NSString *)reuseIdentifier {
    return @"FGVideoMessageCell";
}

- (CGSize)contentSize {
    CGSize size = CGSizeZero;
    BOOL isDir = NO;
    if (![self.snapshotPath isEqualToString:@""] &&
       [[NSFileManager defaultManager] fileExistsAtPath:self.snapshotPath isDirectory:&isDir]) {
        if (!isDir) {
            size = [UIImage imageWithContentsOfFile:self.snapshotPath].size;
        }
    } else {
        size = self.snapshotItem.size;
    }
    if (CGSizeEqualToSize(size, CGSizeZero)) {
        return size;
    }
    
    CGFloat heightMax = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) * 0.4;
    
    if (size.height > size.width) {
        size.width = size.width / size.height * heightMax;
        size.height = heightMax;
    } else {
        size.height = size.height / size.width * heightMax;
        size.width = heightMax;
    }
    return size;
}

- (void)setVideoItem:(FGVideoItem *)videoItem {
    if (!self.uuid) {
        NSAssert(NO, @"必须先设置uuid");
    }
    videoItem.uuid = self.uuid;
    _videoItem = videoItem;
}

- (void)setSnapshotItem:(FGSnapshotItem *)snapshotItem {
    if (!self.uuid) {
        NSAssert(NO, @"必须先设置uuid");
    }
    snapshotItem.uuid = self.uuid;
    _snapshotItem = snapshotItem;
}

- (void)loadThumb {
    if (!self.conversationId) {
        NSAssert(NO, @"必须设置conversationId，方便管理会话文件");
    }
    NSString *path = [_snapshotItem getPath:self.conversationId];
    BOOL isExist = [FGPathTool isExistFile:path];
    if (isExist) {
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        self.thumbImage = image;
        self.thumbProgress = 100;
        return;
    }
    
    if (self.isDownloadingSnapshot) {
        return;
    }
    self.isDownloadingSnapshot = YES;
    __weak typeof(self) ws = self;
    [self.snapshotItem downloadPath:path progress:^(NSUInteger progress) {
        [ws updateThumbProgress:progress];
    } succ:^{
        ws.isDownloadingSnapshot = NO;
        [ws updateThumbProgress:100];
        ws.thumbImage = [UIImage imageWithContentsOfFile:path];
    } fail:^(int code, NSString * _Nonnull msg) {
        ws.isDownloadingSnapshot = NO;
    }];
}

- (void)loadVideo {
    if (!self.conversationId) {
        NSAssert(NO, @"必须设置conversationId，方便管理会话文件");
    }
    NSString *path = [_videoItem getPath:self.conversationId];
    BOOL isExist = [FGPathTool isExistFile:path];
    if (isExist) {
        return;
    }
    
    if (self.isDownloadingVideo) {
        return;
    }
    self.isDownloadingVideo = YES;
    __weak typeof(self) ws = self;
    [self.videoItem downloadPath:path progress:^(NSUInteger progress) {
        [ws updateVideoProgress:progress];
    } succ:^{
        ws.isDownloadingVideo = NO;
        [ws updateThumbProgress:100];
        dispatch_async(dispatch_get_main_queue(), ^{
            ws.videoPath = path;
        });
    } fail:^(int code, NSString * _Nonnull msg) {
        ws.isDownloadingVideo = NO;
    }];
}

- (void)updateThumbProgress:(NSUInteger)progress {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.thumbProgress = progress;
    });
}

- (void)updateVideoProgress:(NSUInteger)progress {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.videoProgress = progress;
    });
}

@end
